import random
import string
import pymongo
import datetime
import sys
import json

if __name__ == '__main__':
    client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
    db = client.cs_recharge
    coll = db.merchant
    if sys.argv[1] == 'add':

        now = datetime.datetime.now()
        mer_id = "M" + now.strftime("%Y%m%d%H%M%s")
        aeskey = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16))
        shakey = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(32))
        name = "信信_10085"
        merchant_type = "支付方"

        #info = {
        #      'merchantid': mer_id,
        #      'AESKEY': aeskey,
        #      'SHAKEY': shakey,
        #      'ip': '[]',
        #      'supplier': 'M2018112613381543210699',
        #      'name': name,
        #      'merchant_type': merchant_type,
              #"supplier": ""
        #}

        print(info)
        coll.insert_one(info)
    elif sys.argv[1] == 'update':
        coll.update({"merchantid": "M2019011016090717140879"}, {
              "$set": { "supplier": "M2018111515481542268096"}})
        # coll.update_one({'merchantid': 'M2018090615441536219886'}, {"$set": {'name': '6688话费', 'merchant_type': '话费方'}})
    elif sys.argv[1] == 'list':
        [print(i) for i in client.cs_recharge.merchant.find()]
